package com.nmf.pc.service;

import java.util.Date;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.nmf.dao.gen.BizSysOfficeMapper;
import com.nmf.dao.gen.BizSysRoleMapper;
import com.nmf.dao.gen.BizSysUserMapper;
import com.nmf.dao.gen.BizSysUserRoleMapper;
import com.nmf.dao.gen.bean.BizSysOffice;
import com.nmf.dao.gen.bean.BizSysUser;
import com.nmf.dao.gen.bean.BizSysUserRoleKey;
import com.nmf.pc.vo.UserRegisterVo;
import com.nmf.site.common.enums.DelFlagEnums;
import com.nmf.site.common.utils.IdGenerater;
import com.nmf.site.modules.sys.service.SystemService;

@Component
public class PcRegisterService {
	Logger log = LoggerFactory.getLogger(PcRegisterService.class);
	@Autowired
	BizSysOfficeMapper bizSysOfficeMapper;
	@Autowired
	BizSysRoleMapper bizSysRoleMapper;
	@Autowired
	BizSysUserMapper bizSysUserMapper;
	@Autowired
	BizSysUserRoleMapper bizSysUserRoleMapper;
	@Autowired
	IdGenerater idGenerater;
	@Value("${yiTuoDanweiRoleId}")
	private String yiTuoDanweiRoleId;
	@Value("${officeParentId}")
	private String officeParentId;
	@Transactional
	public BizSysUser registerUser(UserRegisterVo userRegisterVo) {
		BizSysOffice bizSysOffice = saveOffice(userRegisterVo);
		BizSysUser bizSysUser = saveUser(userRegisterVo,bizSysOffice);
		saveUserRole(bizSysUser);
		return bizSysUser;
	}
	
	private BizSysUserRoleKey saveUserRole(BizSysUser bizSysUser) {
		BizSysUserRoleKey bizSysUserRoleKey = new BizSysUserRoleKey();
		bizSysUserRoleKey.setUserId(bizSysUser.getId());
		bizSysUserRoleKey.setRoleId(yiTuoDanweiRoleId);
		bizSysUserRoleMapper.insertSelective(bizSysUserRoleKey);
		log.info("插入用户权限【"+JSON.toJSONString(bizSysUserRoleKey)+"】成功");
		return bizSysUserRoleKey;
	}
	private BizSysUser saveUser(UserRegisterVo userRegisterVo,BizSysOffice bizSysOffice) {
		BizSysUser bizSysUser = new BizSysUser();
		Date date = new Date();
		
		bizSysUser.setCompanyId(bizSysOffice.getParentId());
		bizSysUser.setCreateBy("1");
		bizSysUser.setCreateDate(date);
		bizSysUser.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value+"");
		bizSysUser.setEmail(userRegisterVo.getMail());
		bizSysUser.setId(idGenerater.genNo("OF"));
		bizSysUser.setMobile(userRegisterVo.getLinkPersonTelNo());
		bizSysUser.setOfficeId(bizSysOffice.getId());
		String enPassword = SystemService.entryptPassword(userRegisterVo.getPassword());
		bizSysUser.setPassword(enPassword);
		bizSysUser.setRemarks(userRegisterVo.getRemark());
		bizSysUser.setUpdateBy("1");
		bizSysUser.setLoginName(userRegisterVo.getLoginName());
		bizSysUser.setName(bizSysOffice.getName());
		bizSysUser.setUpdateDate(date);
		bizSysUserMapper.insertSelective(bizSysUser);
		
		log.info("插入用户【"+JSON.toJSONString(bizSysUser)+"】成功");
		return bizSysUser;
	}
	private BizSysOffice saveOffice(UserRegisterVo userRegisterVo) {
		Date date = new Date();
		Random random = new Random();
		BizSysOffice bizSysOffice = new BizSysOffice();
		bizSysOffice.setAddress(userRegisterVo.getCompAddress());
		bizSysOffice.setAreaId(userRegisterVo.getAreaId());
		bizSysOffice.setCode(random.ints(4).toString());
		bizSysOffice.setCreateDate(date);
		bizSysOffice.setUpdateDate(date);
		bizSysOffice.setDelFlag(DelFlagEnums.DEL_FLAG_NORMAL.value+"");
		bizSysOffice.setDeputyPerson(userRegisterVo.getLinkPersonName());
		bizSysOffice.setEmail(userRegisterVo.getMail());
		bizSysOffice.setId(idGenerater.genNo("OF"));
		bizSysOffice.setName(userRegisterVo.getCompName());
		
		bizSysOffice.setParentId(officeParentId+",");
		bizSysOffice.setParentIds("0,"+officeParentId+",");
		
		bizSysOffice.setPhone(userRegisterVo.getLinkTelNo());
		bizSysOffice.setRemarks(userRegisterVo.getRemark());
		bizSysOffice.setType(2+"");
		bizSysOffice.setCreateBy(1+"");
		bizSysOffice.setSort(20l);
		bizSysOffice.setGrade("2");
		bizSysOffice.setUpdateBy("1");
		bizSysOfficeMapper.insertSelective(bizSysOffice);
		log.info("插入公司【"+JSON.toJSONString(bizSysOffice)+"】成功");
		
		return bizSysOffice;
	}
}
